package edu.ciee.tz.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.tz.model.Laiyuan;
import edu.ciee.tz.model.Zhibie;

@Component("laiyuanDao")
public class LaiyuanDAO {

	public List<Laiyuan> search(Laiyuan l) throws Exception {

		String query = "where 1=1";

		if (l.getId() != 0) 
			query = query + " and id like '%" + l.getId() + "%'";// this.getID()
		if (l.getName() != null) 
			query = query + " and name like '%" + l.getName() + "%'"; // this.getName()
		if (l.getProduct_type() != null) 
			query = query + " and product_type like '%" + l.getProduct_type() + "%'"; // this.getName()
		if (l.getTele() != null) 
			query = query + " and tele like '%" + l.getTele() + "%'"; // this.getName()
		
		@SuppressWarnings("unchecked")
		List<Laiyuan> laiyuan_list = hibernateTemplate.find("from Laiyuan u "+ query);

		return laiyuan_list;
	}
	
	public List<Laiyuan> searchAll() throws Exception {
		@SuppressWarnings("unchecked")
		List<Laiyuan> laiyuan_list = hibernateTemplate.find("from Laiyuan u ");
		
		Map map=new HashMap();
		System.out.println("size:"+laiyuan_list.size());
		for(int i=0;i<laiyuan_list.size();i++){
			Laiyuan ly=new Laiyuan();
			ly=(Laiyuan) laiyuan_list.get(i);
			System.out.println(laiyuan_list.get(i));
			
			//HashMap m=zhibie_list.get(i);			
			//HashMap m=new HashMap();
			//m.put("id",zb.getId())	;
			//m.put("name", zb.getName());
			//map.put(m.get("id"), m.get("name"));
			//map.put("id",zb.getId());
			//map.put("name", zb.getName());
			map.put(ly.getId(), ly.getName());
		}			
		ActionContext.getContext().getSession().put("laiyuanMap", map);
		
		return laiyuan_list;
	}
	
	public void insert(Laiyuan l) throws Exception {
		this.getHibernateTemplate().save(l);
	}

	public void delete(Laiyuan l) throws Exception {
		this.getHibernateTemplate().delete(l);

	}

	public void update(Laiyuan l) throws Exception {
		this.getHibernateTemplate().update(l);
	}

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
